package cn.broadsense.common.validator;

import cn.broadsense.common.annotation.BigDecimalRange;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

/**
 * @author huangjinshi
 * @description
 * @createDate 2024-09-20 10:54:04
 */
public class BigDecimalRangeValidator implements ConstraintValidator<BigDecimalRange, BigDecimal> {

    private double min;
    private double max;

    @Override
    public void initialize(BigDecimalRange constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return value.doubleValue() >= min && value.doubleValue() <= max;
    }
}
